<template>
  <page-layout>
    <div slot="toolbar">
      <el-button size="small" round type="primary" @click="newNews">新增新闻</el-button>
    </div>
    <div class="card-header">新闻管理</div>
    <el-card class="box-card">
      <el-card class="box-card">
      <el-row>
        <el-col :span="24">
            <el-form ref="searchNewsCondition" :model="searchNewsCondition" label-width="80px" inline>
              <el-form-item label="标题">
                <el-input v-model="searchNewsCondition.title" style="width: 400px;"/>
              </el-form-item>
              <el-form-item label="来源">
                <el-input v-model="searchNewsCondition.source" style="width: 400px;"/>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="searchNewsCondition.status" placeholder="请选择" style="width: 400px;">
                  <el-option
                    v-for="item in NewsStatus"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="查询时间">
                <el-date-picker
                  v-model="value4"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="查询开始日期"
                  end-placeholder="查询截至日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="search(1)">查询</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="box-card">
            <el-table
              :data="newsList.content" border stripe
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="55">
              </el-table-column>

              <el-table-column prop="title" label="标题" >
                <template slot-scope="scope">
                  <span style="color: red" v-if="scope.row.recommendSign">[{{scope.row.recommendSign}} - {{scope.row.sort}}]</span>
                  <span>{{scope.row.title}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="createTime"
                label="发布时间" width="180">
              </el-table-column>
              <el-table-column prop="image" label="缩略图" width="210">
                <template slot-scope="scope">
                  <image-cover v-if="scope.row.cover1" :thumb.sync="scope.row.cover1" width="96px" height="63px" size-type="contain"></image-cover>
                  <span v-else>暂时无图</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="source"
                label="来源" width="120">
              </el-table-column>
              <el-table-column
                prop="score"
                label="积分" width="80">
              </el-table-column>
              <el-table-column
                prop="readNum"
                label="阅读量" width="120">
              </el-table-column>
              <el-table-column
                prop="discussNum"
                label="评论数" width="120">
              </el-table-column>
              <el-table-column
                prop="createUserName"
                label="创建人" width="120">
              </el-table-column>
              <el-table-column
                prop="status"
                label="状态" width="120">
                <template slot-scope="scope">
                  <span>{{scope.row.status| keyText('NewsStatus')}}</span>

                </template>
              </el-table-column>
              <el-table-column label="操作" width="250">
                <template slot-scope="scope">
                  <el-button size="mini" type="text"  @click="preNews(scope.row)"> 预览 </el-button>
                  <el-button size="mini" type="text" v-if="scope.row.status===1&&adminInfo.userRole==='ADMIN'&&!scope.row.recommendSign"  @click="sign(scope.row)"> 推荐 </el-button>
                  <el-button size="mini" type="text" v-if="scope.row.status===1&&adminInfo.userRole==='ADMIN'&&scope.row.recommendSign"  @click="cancelSign(scope.row)"> 取消推荐 </el-button>
                  <el-button size="mini" type="text" v-if="adminInfo.userRole==='ADMIN'&&scope.row.status===3"  @click="auditNews(scope.row)"> 审核 </el-button>
                  <el-button size="mini" type="text" v-if="adminInfo.userRole!=='ADMIN'&&(scope.row.status===0||scope.row.status===2)"  @click="commitAuditNews(scope.row)"> 提交审核 </el-button>
                  <el-button size="mini" v-if="scope.row.status===0||scope.row.status===2||scope.row.status===4" @click="editNews(scope.$index, scope.row)" type="text">编辑</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===0&&adminInfo.userRole==='ADMIN'"  @click="save(scope.row,1)">发布</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===1&&adminInfo.userRole==='ADMIN'"  @click="save(scope.row,2)">下架</el-button>
                  <el-button  size="mini" type="text"  v-if="adminInfo.userRole==='ADMIN'"  @click="updateTime(scope.row,3)">更新发布时间</el-button>
                  <el-button  size="mini" type="text"    @click="seeDiscuss(scope.row)">查看评论</el-button>
                  <el-button v-if="scope.row.status===4" size="mini" type="text"  @click="seeReason(scope.row)"> 查看原因 </el-button>
                  <el-button size="mini" type="text" @click="deleteNews(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="newsList.totalElements"
                :page-size="newsList.size"
                :current-page.sync="currentNewsPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
    </el-card>
    <el-dialog :visible.sync="showNewsEditor" title="添加/编辑 新闻" width="80%" >
      <news-editor @close="onNewsEditorClose" :is-close="showNewsEditor"></news-editor>
    </el-dialog>
    <el-dialog title="预览" :visible.sync="showPreview" width="50%">
      <pre-news @close="onShowPreviewClose"  :is-close="showPreview"></pre-news>
    </el-dialog>
    <el-dialog title="推荐" :visible.sync="showSign" width="500px">
      <el-form ref="form" :model="currentSign" label-width="100px" class="demo-form">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-card class="box-card">
              <el-form-item label="推荐标识" prop="recommendSign">
                <el-input v-model="currentSign.recommendSign" placeholder="置顶、热等"></el-input>
              </el-form-item>
              <el-form-item label="推荐位置" prop="sort">
                <el-input v-model="currentSign.sort" placeholder="1"></el-input>
              </el-form-item>
              <el-form-item>
                <span style="float: right;">
                    <el-button type="primary"  @click="saveSign()">保存</el-button>
                     <el-button @click="showSign=false">返回</el-button>
               </span>
              </el-form-item>
            </el-card>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog title="审核新闻" :visible.sync="showAudit" width="500px">
      <el-form ref="auditForm" :rules="rules" :model="auditForm" label-width="100px" class="demo-form">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-card class="box-card">
              <el-form-item label="拒绝原因" prop="reason">
                <el-input
                  type="textarea"
                  :rows="7"
                  placeholder="请输入拒绝原因"
                  v-model="auditForm.reason">
                </el-input>
              </el-form-item>
              <el-form-item>
                <span style="float: right;">
                     <el-button type="primary"  @click="saveAuditNews(1)">通过</el-button>
                     <el-button type="danger"  @click="saveAuditNews(4)">拒绝</el-button>
                     <el-button @click="showAudit=false">返回</el-button>
               </span>
              </el-form-item>
            </el-card>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import NewsEditor from "./news-editor";
import { mapGetters } from "vuex";
import ImageCover from "@/components/image-cover";
import PreNews from "./pre-news";
import Status from "@/libs/status";
import Moment from "moment";
import _ from "lodash";
export default {
  created() {
    this.search(1);
  },
  mounted() {
    this.NewsStatus = _.clone(Status.NewsStatus);
    this.NewsStatus.unshift({ text: "全部" });
  },
  data() {
    return {
      showAudit: false,
      showSign: false,
      currentNewsPage: 1,
      showNewsEditor: false,
      showPreview: false,
      curPreObj: {},
      NewsStatus: [],
      value4: [],
      currentSign: {},
      auditForm: { reason: "" },
      rules: {
        reason: [{ required: true, message: "请输入原因", trigger: "blur" }]
      }
    };
  },
  computed: {
    ...mapGetters({
      adminInfo: "adminInfo",
      newsList: "newsList",
      searchNewsCondition: "searchNewsCondition"
    })
  },
  methods: {
    seeReason(row) {
      this.$alert(row.reason, "驳回原因", {
        confirmButtonText: "关闭",
        callback: action => {}
      });
    },
    seeDiscuss(row) {
      this.$router.push({
        name: "discuss",
        query: { newsId: row.id }
      });
    },
    //取消推荐
    cancelSign(row) {
      this.$alert("确认取消推荐", "提示", {
        confirmButtonText: "确定",
        callback: action => {
          this.$store
            .dispatch("cancelRecommenNewsS", { newsId: row.id })
            .then(ret => {
              this.$notify({
                title: "提交成功",
                type: "success"
              });
              this.getPageChange(this.currentNewsPage);
            });
        }
      });
    },
    getPageChange(val) {
      this.searchNewsCondition.startTime = "";
      this.searchNewsCondition.endTime = "";
      if (this.value4 && this.value4.length > 0) {
        this.searchNewsCondition.startTime = Moment(this.value4[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.searchNewsCondition.endTime = Moment(this.value4[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      console.log(this.searchNewsCondition);
      var tem = Object.assign({ pageIndex: val - 1 }, this.searchNewsCondition);
      console.log(tem);
      this.$store.dispatch("getNewsPageS", tem).then(ret => {});
    },
    search(val) {
      this.getPageChange(val);
    },
    //推荐
    sign(row) {
      this.showSign = true;
      this.currentSign.newsId = row.id;
      this.currentSign.recommendSign = row.recommendSign;
      this.currentSign.sort = row.sort;
      if (this.currentSign.sort === undefined) {
        this.currentSign.sort = 1;
      }
    },
    //保存推荐信息
    saveSign() {
      this.$store.dispatch("recommenNewsS", this.currentSign).then(ret => {
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.getPageChange(this.currentNewsPage);
        this.showSign = false;
      });
    },
    //弹出审核
    auditNews(row) {
      this.showAudit = true;
      this.auditForm.id = row.id;
    },
    //保存审核结果
    saveAuditNews(status) {
      this.auditForm.status = status;
      if (status === 4) {
        this.$refs["auditForm"].validate(valid => {
          if (valid) {
            this.commitUpdateNews();
          }
        });
      } else {
        this.commitUpdateNews();
      }
    },
    //提交更新
    commitUpdateNews() {
      this.$store.dispatch("updateNewsS", this.auditForm).then(ret => {
        this.getPageChange(this.currentNewsPage);
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.showAudit = false;
      });
    },
    //弹出提交审核
    commitAuditNews(row) {
      this.$confirm("是否确认提交审核?", "提示", {})
        .then(() => {
          var tem = _.clone(row);
          tem.status = 3;
          this.$store.dispatch("addOrUpdateNewsS", tem).then(ret => {
            this.getPageChange(this.currentNewsPage);
            this.$notify({
              title: "提交成功",
              type: "success"
            });
          });
        })
        .catch(() => {});
    },
    deleteNews(row) {
      this.$confirm("确认删除当前新闻?", "提示", {})
        .then(() => {
          this.$store.dispatch("deleteNewsS", { id: row.id }).then(ret => {
            this.getPageChange(this.currentNewsPage);
            this.$notify({
              title: "删除成功",
              type: "success"
            });
          });
        })
        .catch(() => {});
    },
    preNews(row) {
      console.log(row.id);
      this.$store.dispatch("getNewsS", row.id).then(ret => {});
      this.showPreview = true;
    },
    newNews() {
      this.$store.commit("INIT_CURRENT_NEWS");
      this.showNewsEditor = true;
    },
    editNews(index, row) {
      this.$store.dispatch("getNewsS", row.id).then(ret => {});
      this.showNewsEditor = true;
    },
    onNewsEditorClose() {
      this.showNewsEditor = false;
      this.getPageChange(this.currentNewsPage);
    },
    save(row, status) {
      row.status = status;
      this.$store.dispatch("addOrUpdateNewsS", row).then(ret => {
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.getPageChange(this.currentNewsPage);
      });
    },
    updateTime(row, status) {
      var para = {};
      para.id = row.id;
      para.status = status;
      this.$store.dispatch("updateTime", para).then(ret => {
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.getPageChange(this.currentNewsPage);
      });
    },
    onShowPreviewClose() {
      this.showPreview = false;
      this.getPageChange(this.currentNewsPage);
    }
  },
  components: {
    PageLayout,
    NewsEditor,
    ImageCover,
    PreNews
  }
};
</script>
<style>
</style>
